use "build/output/main_panel.dta", clear 

tsset coin_id date
sort coin_id date

******************************************************************************** 
* labels
********************************************************************************
label var margin_aprFTX "\$R_{t}\$"

capture program drop list_table_end
program define list_table_end
	matrix list e(first)
	local SWF= e(first)[rownumb(e(first),"SWF"),1]
	estadd scalar SWF=`SWF', replace
	estadd local Obs `e(N)'
	estadd scalar R2 = round(`e(r2)',0.01), replace
	estadd scalar adjR2 = round(`e(r2_a)',0.01), replace
	estadd local space " "
	estadd local hasFE "`1'"	
end



eststo clear
eststo reg1: ivreg2 margin_aprFTX (funding_apr = HouseholdRating) iv_btc d_ln_face_bps i.dow i.month i.year if coin == "USDT", r first savefirst ffirst savefprefix(reg1_first_) cluster(wdate)
list_table_end "Yes" 
estadd scalar waldF = round(`e(widstat)',0.01), replace

eststo reg2: ivreg2 margin_aprFTX (funding_apr = HouseholdRating) l.margin_aprFTX  iv_btc d_ln_face_bps i.dow i.month i.year if coin == "USDT", r  first savefirst ffirst savefprefix(reg2_first_) cluster(wdate)
list_table_end "Yes" 
estadd scalar waldF = round(`e(widstat)',0.01), replace

eststo reg3: ivreg2 margin_aprFTX (funding_apr = HouseholdRating) iv_btc d_ln_face_bps i.dow i.month i.year if coin == "DAI", r  first savefirst ffirst savefprefix(reg3_first_) cluster(wdate)
list_table_end "Yes" 
estadd scalar waldF = round(`e(widstat)',0.01), replace

eststo reg4: ivreg2 margin_aprFTX (funding_apr = HouseholdRating) l.margin_aprFTX iv_btc d_ln_face_bps i.dow i.month i.year if coin == "DAI", r  first savefirst ffirst savefprefix(reg4_first_) cluster(wdate)
list_table_end "Yes" 
estadd scalar waldF = round(`e(widstat)',0.01), replace

esttab, star(* 0.10 ** 0.05 *** 0.01) drop(*month  *year _cons  *dow) scalar(widstat)


******************************************************************************** 
* Panel B: First Stage
********************************************************************************

esttab reg1_first_funding_apr ///
	reg2_first_funding_apr ///
	reg3_first_funding_apr ///
	reg4_first_funding_apr ///
	using "analysis/output_tables/regression_iv_first_stage.tex", replace booktabs label ///
	prehead(\begin{tabular}{l S S S S } \midrule ///
		\multicolumn{5}{l}{\textbf{Panel B: First Stage}} \\ \midrule ///
		& \multicolumn{4}{c}{Futures Funding Rate} \\  ///
		& \multicolumn{2}{c}{USDT} & \multicolumn{2}{c}{DAI} \\ ///
		\cmidrule(lr){2-3} \cmidrule(lr){4-5} ) ///
 	posthead(\midrule) postfoot(\end{tabular})  ///
	b(3) t(3) star(* 0.10 ** 0.05 *** 0.01) drop (*month *year _cons  *dow) ///
	stats(N space , fmt(%12.0fc 2 ) ///
 		labels(`"\$N\$"'  `" "' ) ///
 		layout("\multicolumn{1}{c}{@}" ))  ///
 	varlabels(_cons "Constant" HouseholdRating "\text{Rating}_t" L.margin_aprFTX "\$R_{t-1}\$" d_ln_face_bps "\$\Delta \ln(s_{i,t})\$" iv_btc "\$\text{Bitcoin Implied Volatility}_{t}\$") ///
	nomtitles style(tex) substitute(\_ _) 
	

******************************************************************************** 
* Panel A: Second Stage
********************************************************************************
		
esttab reg1 reg2 reg3 reg4   ///
	using "analysis/output_tables/regression_iv_second_stage.tex", replace booktabs label  ///
 	prehead(\begin{tabular}{l S S S S} \midrule ///
		\multicolumn{5}{l}{\textbf{Panel A: Second Stage}} \\ \midrule ///
 	    & \multicolumn{4}{c}{Lending Rate \$R_{t}\$}  \\ ///
		& \multicolumn{2}{c}{USDT} & \multicolumn{2}{c}{DAI} \\ ///
		\cmidrule(lr){2-3} \cmidrule(lr){4-5} ) 	///
  	posthead(\midrule) postfoot(\midrule\end{tabular})  ///
 	b(3) t(3) star(* 0.10 ** 0.05 *** 0.01)  drop(*month *year _cons  *dow)  ///
 	stats(N space waldF hasFE, fmt(%12.0fc 2 %12.2fc %12.0fc) ///
		labels(`"\$N\$"'  `" "' `"\$F\$-stat"' `"Time FE"' )  ///
 		layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" ))  ///
 	varlabels(_cons "Constant" funding_apr "\$\widehat{\text{Futures Funding Rate}_{t}}\$" ///
		L.margin_aprFTX "\$R_{t-1}\$" HouseholdRating "\text{Rating}_t" d_ln_face_bps "\$\Delta \ln(s_{i,t})\$" iv_btc "\$\text{Bitcoin Implied Volatility}_{t}\$") ///
 	nomtitles style(tex) substitute(\_ _) 
	
		
******************************************************************************** 
* Panel C: OLS
********************************************************************************
capture program drop list_table_end
program define list_table_end
	estadd local Obs `e(N)'
	estadd scalar R2 = round(`e(r2)',0.01), replace
	estadd scalar adjR2 = round(`e(r2_a)',0.01), replace
	estadd local hasFE "`1'"
end

eststo clear
eststo reg1ols: reg margin_aprFTX funding_apr iv_btc d_ln_face_bps i.dow i.month i.year if coin == "USDT", vce(cluster wdate)
list_table_end "Yes" 
eststo reg2ols: reg margin_aprFTX funding_apr l.margin_aprFTX iv_btc d_ln_face_bps i.dow i.month i.year if coin == "USDT", vce(cluster wdate)
list_table_end "Yes" 
eststo reg3ols: reg margin_aprFTX funding_apr iv_btc d_ln_face_bps i.dow i.month i.year if coin == "DAI", vce(cluster wdate)
list_table_end "Yes" 
eststo reg4ols: reg margin_aprFTX funding_apr l.margin_aprFTX iv_btc d_ln_face_bps i.dow i.month i.year if coin == "DAI", vce(cluster wdate)
list_table_end "Yes" 
esttab reg1ols reg2ols reg3ols reg4ols, drop (*month *year _cons  *dow)
esttab reg1ols reg2ols reg3ols reg4ols ///
	using "analysis/output_tables/regression_OLS.tex", replace booktabs label ///
	prehead(\begin{tabular}{l S S S S} \midrule \multicolumn{5}{l}{\textbf{Panel C: OLS}} \\ \midrule ///
		   & \multicolumn{4}{c}{Lending Rate \$R_{t}\$} \\ ///
		    & \multicolumn{2}{c}{USDT}& \multicolumn{2}{c}{DAI} \\ ///
			\cmidrule(lr){2-3} \cmidrule(lr){4-5}) ///
 	posthead(\midrule) postfoot(\bottomrule\end{tabular})  ///
	b(3) t(3)  star(* 0.10 ** 0.05 *** 0.01)  drop (*month *year _cons  *dow) ///
	stats(N  space hasFE, fmt(%12.0fc 2 %12.0fc) ///
		labels(`"\$N\$"'  `" "' `"Time FE"') ///
		layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}"))  ///
	varlabels(_cons "Constant" funding_apr "\$\text{Futures Funding Rate}_{t}\$" d_ln_face_bps "\$\Delta \ln(s_{i,t})\$" iv_btc "\$\text{Bitcoin Implied Volatility}_{t}\$" ///		
		HouseholdRating "Rating" L.margin_aprFTX "\$R_{t-1}\$") ///
	nomtitles style(tex) substitute(\_ _) 
	

******************************************************************************** 
* Appendix: Placebo, use 1 day, and 1 week and 4 weeks
********************************************************************************
eststo clear

eststo reg1placebo: ivreg2 margin_aprFTX (funding_apr = f1.HouseholdRating) l.margin_aprFTX  iv_btc d_ln_face_bps i.dow i.month i.year if coin == "USDT", r first savefirst ffirst cluster(wdate)
estadd local hasFE "Yes"
estadd scalar waldF = round(`e(widstat)',0.01), replace

eststo reg2placebo: ivreg2 margin_aprFTX (funding_apr = f7.HouseholdRating) l.margin_aprFTX  iv_btc d_ln_face_bps i.dow i.month i.year if coin == "USDT", r first savefirst ffirst cluster(wdate)
estadd local hasFE "Yes"
estadd scalar waldF = round(`e(widstat)',0.01), replace

eststo reg3placebo: ivreg2 margin_aprFTX (funding_apr = f28.HouseholdRating) l.margin_aprFTX  iv_btc d_ln_face_bps i.dow i.month i.year if coin == "USDT", r first savefirst ffirst cluster(wdate)
estadd local hasFE "Yes"
estadd scalar waldF = round(`e(widstat)',0.01), replace

eststo reg4placebo: ivreg2 margin_aprFTX (funding_apr = f1.HouseholdRating) l.margin_aprFTX  iv_btc d_ln_face_bps i.dow i.month i.year if coin == "DAI", r first savefirst ffirst cluster(wdate)
estadd local hasFE "Yes"
estadd scalar waldF = round(`e(widstat)',0.01), replace

eststo reg5placebo: ivreg2 margin_aprFTX (funding_apr = f7.HouseholdRating) l.margin_aprFTX  iv_btc d_ln_face_bps i.dow i.month i.year if coin == "DAI", r first savefirst ffirst cluster(wdate)
estadd local hasFE "Yes"
estadd scalar waldF = round(`e(widstat)',0.01), replace

eststo reg6placebo: ivreg2 margin_aprFTX (funding_apr = f28.HouseholdRating) l.margin_aprFTX  iv_btc d_ln_face_bps i.dow i.month i.year if coin == "DAI", r first savefirst ffirst cluster(wdate)
estadd local hasFE "Yes"
estadd scalar waldF = round(`e(widstat)',0.01), replace


esttab,  label  drop(*month *year _cons  *dow) scalar(widstat) star(* 0.10 ** 0.05 *** 0.01)

esttab reg1placebo reg2placebo reg3placebo reg4placebo reg5placebo reg6placebo  ///
	using "analysis/output_tables/regression_iv_placebo.tex", replace booktabs label  ///
 	prehead(\begin{tabular}{l S S S S S S} \midrule ///
		& \multicolumn{6}{c}{\text{Lending Rate \$R_{t}\$}} \\ ///
 	    & \multicolumn{3}{c}{USDT} & \multicolumn{3}{c}{DAI} \\ ///
		& \multicolumn{1}{c}{1 Day} & \multicolumn{1}{c}{1 Week} & \multicolumn{1}{c}{4 Weeks} & \multicolumn{1}{c}{1 Day} & \multicolumn{1}{c}{1 Week} & \multicolumn{1}{c}{4 Weeks} \\ ///
		\cmidrule(lr){2-4} \cmidrule(lr){5-7} ) ///
  	posthead(\midrule) postfoot(\midrule\end{tabular})  ///
 	b(3) t(3) star(* 0.10 ** 0.05 *** 0.01)  drop(*month *year _cons  *dow)  ///
 	stats(N waldF hasFE, fmt(%12.0fc 2 %12.2fc %12.0fc) ///
 		labels(`"\$N\$"' `"\$F\$-stat"' `"Time FE"' ) ///
 		layout("\multicolumn{1}{c}{@}" ///
 			 "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}"))  ///
 	varlabels(_cons "Constant" funding_apr "\$\widehat{\text{Futures Funding Rate}_{t}}\$" ///
		L.margin_aprFTX "\$R_{t-1}\$" HouseholdRating "\text{Rating}_t" d_ln_face_bps "\$\Delta \ln(s_{i,t})\$" iv_btc "\$\text{Bitcoin Implied Volatility}_{t}\$") ///
 	nomtitles style(tex) substitute(\_ _) 
	
	
******************************************************************************** 
* Appendix: CLI
********************************************************************************

eststo clear
eststo reg1_cli: ivreg2 margin_aprFTX (funding_apr = HouseholdRating_times_cli) iv_btc d_ln_face_bps i.dow i.month i.year if coin == "USDT", r first savefirst ffirst savefprefix(reg1_first_) cluster(wdate)
list_table_end "Yes" 
estadd scalar waldF = round(`e(widstat)',0.01), replace

eststo reg2_cli: ivreg2 margin_aprFTX (funding_apr = HouseholdRating_times_cli) l.margin_aprFTX  iv_btc d_ln_face_bps i.dow i.month i.year if coin == "USDT", r  first savefirst ffirst savefprefix(reg2_first_) cluster(wdate)
list_table_end "Yes" 
estadd scalar waldF = round(`e(widstat)',0.01), replace

eststo reg3_cli: ivreg2 margin_aprFTX (funding_apr = HouseholdRating_times_cli) iv_btc d_ln_face_bps i.dow i.month i.year if coin == "DAI", r  first savefirst ffirst savefprefix(reg3_first_) cluster(wdate)
list_table_end "Yes" 
estadd scalar waldF = round(`e(widstat)',0.01), replace

eststo reg4_cli: ivreg2 margin_aprFTX (funding_apr = HouseholdRating_times_cli) l.margin_aprFTX iv_btc d_ln_face_bps i.dow i.month i.year if coin == "DAI", r  first savefirst ffirst savefprefix(reg4_first_) cluster(wdate)
list_table_end "Yes" 
estadd scalar waldF = round(`e(widstat)',0.01), replace

esttab,  label  drop(*month *year _cons  *dow) scalar(widstat) star(* 0.10 ** 0.05 *** 0.01)

esttab reg1_cli reg2_cli reg3_cli reg4_cli  ///
	using "analysis/output_tables/regression_iv_cli.tex", replace booktabs label  ///
 	prehead(\begin{tabular}{l S S S S} \midrule ///
		\multicolumn{5}{l}{\textbf{Instrument: Household Rating \$\times\$ Championship Leverage Index}} \\ \midrule ///
 	    & \multicolumn{4}{c}{Lending Rate \$R_{t}\$}  \\ ///
		& \multicolumn{2}{c}{USDT} & \multicolumn{2}{c}{DAI} \\ ///
		\cmidrule(lr){2-3} \cmidrule(lr){4-5} ) 	///
  	posthead(\midrule) postfoot(\midrule\end{tabular})  ///
 	b(3) t(3) star(* 0.10 ** 0.05 *** 0.01)  drop(*month *year _cons  *dow)  ///
 	stats(N waldF hasFE, fmt(%12.0fc 2 %12.2fc %12.0fc) ///
		labels(`"\$N\$"' `"\$F\$-stat"' `"Time FE"' )  ///
 		layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}"))  ///
 	varlabels(_cons "Constant" funding_apr "\$\widehat{\text{Futures Funding Rate}_{t}}\$" ///
		L.margin_aprFTX "\$R_{t-1}\$" HouseholdRating "\text{Rating}_t" d_ln_face_bps "\$\Delta \ln(s_{i,t})\$" iv_btc "\$\text{Bitcoin Implied Volatility}_{t}\$") ///
 	nomtitles style(tex) substitute(\_ _) 
	
	
// for text
reg HouseholdRating cli if coin == "USDT", r
